#include "Buzzer.h"

u16 code hz[] = {1047, 1175, 1319, 1397, 1568, 1760, 1976, 2093};



static void GPIO_config(){
   
	GPIO_InitTypeDef	GPIO_InitStructure;		//结构定义
	GPIO_InitStructure.Pin  = GPIO_Pin_0;		//指定要初始化的IO,
	GPIO_InitStructure.Mode = GPIO_PullUp;	//指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P0, &GPIO_InitStructure);//初始化

}

PWMx_Duty dutyB;
static void PWM_config(u16 hz_val)
{
  PWMx_InitDefine		PWMx_InitStructure;
	
	u16 period = (MAIN_Fosc / hz_val);	// 计算每个周期的计数值
	
  // 配置PWM5
  PWMx_InitStructure.PWM_Mode    		= CCMRn_PWM_MODE1;	//模式,		CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
  PWMx_InitStructure.PWM_Duty   	 	= (u16)(period * 0.1);	//PWM占空比时间, 0~Period
  PWMx_InitStructure.PWM_EnoSelect    = ENO5P;			//输出通道选择,	ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
  PWM_Configuration(PWM5, &PWMx_InitStructure);			//初始化PWM,  PWMA,PWMB

  // 配置PWMB
  PWMx_InitStructure.PWM_Period   = period - 1;			//周期时间,   0~65535
  PWMx_InitStructure.PWM_DeadTime = 0;					//死区发生器设置, 0~255
  PWMx_InitStructure.PWM_MainOutEnable= ENABLE;			//主输出使能, ENABLE,DISABLE
  PWMx_InitStructure.PWM_CEN_Enable   = ENABLE;			//使能计数器, ENABLE,DISABLE
  PWM_Configuration(PWMB, &PWMx_InitStructure);			//初始化PWM通用寄存器,  PWMA,PWMB

  // 切换PWM通道
  PWM5_SW(PWM5_SW_P00);					//PWM5_SW_P20,PWM5_SW_P17,PWM5_SW_P00,PWM5_SW_P74

  // 初始化PWMB的中断
  NVIC_PWM_Init(PWMB,DISABLE,Priority_0);
}

static void PWM_config_stop()

{
  PWMx_InitDefine		PWMx_InitStructure;
  // 配置PWM5
  PWMx_InitStructure.PWM_Mode    		= CCMRn_PWM_MODE1;	//模式,		CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
  PWMx_InitStructure.PWM_Duty   	 	= 0;	//PWM占空比时间, 0~Period
  PWMx_InitStructure.PWM_EnoSelect    = ENO5P;			//输出通道选择,	ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
  PWM_Configuration(PWM5, &PWMx_InitStructure);			//初始化PWM,  PWMA,PWMB

  // 配置PWMB
  PWMx_InitStructure.PWM_Period   = 0;			//周期时间,   0~65535
  PWMx_InitStructure.PWM_DeadTime = 0;					//死区发生器设置, 0~255
  PWMx_InitStructure.PWM_MainOutEnable= ENABLE;			//主输出使能, ENABLE,DISABLE
  PWMx_InitStructure.PWM_CEN_Enable   = ENABLE;			//使能计数器, ENABLE,DISABLE
  PWM_Configuration(PWMB, &PWMx_InitStructure);			//初始化PWM通用寄存器,  PWMA,PWMB

  // 切换PWM通道
  PWM5_SW(PWM5_SW_P00);					//PWM5_SW_P20,PWM5_SW_P17,PWM5_SW_P00,PWM5_SW_P74

  // 初始化PWMB的中断
  NVIC_PWM_Init(PWMB,DISABLE,Priority_0);
}

static void Timer_config()
{
    TIM_InitTypeDef		TIM_InitStructure;						//结构定义
  //定时器0做16位自动重装, 中断频率为1000HZ
  TIM_InitStructure.TIM_Mode      = TIM_16BitAutoReload;	//指定工作模式,   TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
  TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T;		//指定时钟源,     TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
  TIM_InitStructure.TIM_ClkOut    = DISABLE;				//是否输出高速脉冲, ENABLE或DISABLE
  TIM_InitStructure.TIM_Value     = 65536UL-1;		//初值,
  TIM_InitStructure.TIM_Run       = ENABLE;				//是否初始化后启动定时器, ENABLE或DISABLE
  Timer_Inilize(Timer3,&TIM_InitStructure);				//初始化Timer0	  Timer0,Timer1,Timer2,Timer3,Timer4
   Timer_Inilize(Timer3,&TIM_InitStructure);				//初始化Timer0	  Timer0,Timer1,Timer2,Timer3,Timer4
  NVIC_Timer3_Init(ENABLE,Priority_3);		//中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
   NVIC_Timer3_Init(ENABLE,Priority_3);		//中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
}

long count =5;

void timer2_callback(){
	count++;
	count%=7;
}


void Buzzer_init(){
    GPIO_config();
    Timer_config();
}
void Buzzer_play(){
	
    PWM_config(hz[count]);
}

void Buzzer_stop(){
    PWM_config_stop();
}






























